<template>
  <div style="margin: 40px">
    <el-form :inline="true" :model="queryTeacher" class="demo-form-inline">
      <el-form-item label="讲师名称">
        <el-input v-model="queryTeacher.user"></el-input>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="queryTeacher.region" placeholder="请选择头衔" clearable>
          <el-option label="高级讲师" value="1"></el-option>
          <el-option label="首席讲师" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="添加时间">
        <el-date-picker
          v-model="queryTeacher.beginDate"
          type="datetime"
          placeholder="开始时间"
        >
        </el-date-picker>
        <el-date-picker
          v-model="queryTeacher.endDate"
          type="datetime"
          placeholder="结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onQuery(1)">查询</el-button>
        <el-button @click="queryTeacher={}">清空</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border fit>
      <el-table-column label="序号" align="center" width="95" v-slot="scope">
        {{ scope.$index + 1 }}
      </el-table-column>
      <el-table-column prop="name" label="姓名" align="center" width="95"/>
      <el-table-column label="头衔" align="center" width="95" v-slot="scope">
        {{ scope.row.level === 1 ? '高级讲师' : '首席讲师' }}
      </el-table-column>
      <el-table-column prop="intro" label="资历" align="center"/>
      <el-table-column prop="sort" label="排序" align="center" width="95"/>
      <el-table-column prop="eduCreate" label="添加时间" align="center" width="200"/>
      <el-table-column prop="eduModified" label="修改时间" align="center" width="200"/>
      <el-table-column label="操作" align="center" width="200"  v-slot="scope">
        <el-button type="primary" @click="toUpdate(scope.row.id)">编辑</el-button>
        <el-button type="danger" @click="onDel(scope.row.id)">删除</el-button>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 20px"
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :current-page="pageNum"
      @current-change="onQuery"
      :total="total"/>
  </div>
</template>

<script>
import {teaDelete, teaGetById, teaPage} from '@/api/teacher'

export default {
  data() {
    return {
      queryTeacher: {},
      pageNum: 1,
      pageSize: 2,
      total: 0,
      tableData: []
    }
  },
  created() {
    this.onQuery(1)
  },
  methods: {
    onQuery(pageNum) {
      this.pageNum = pageNum
      teaPage(this.queryTeacher, this.pageNum, this.pageSize).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    toUpdate(id){
      teaGetById(id).then(res=>{
        this.$router.push({path:'/teacher/save',query:{teacher:res.data}})
      })
    },
    onDel(id){
      this.$confirm('此操作将永久删除该讲师, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        teaDelete(id).then(res=>{
          if (res.code===200){
            this.$message.success('删除成功!');
          }else {
            this.$message.error('删除失败!');
          }
          this.onQuery(this.pageNum)
        })

      }).catch(() => {
        this.$message.info('已取消删除')
      });
    }

  }
}
</script>

<style>

</style>
